<?php
namespace app\admin\controller;

use think\Session;
use think\Image;
use app\common\model\WxUser as WxUserModel;
use app\common\util\WechatThirdUtil;
use app\common\controller\AdminBase;
use app\common\model\Subscribereply as SubscribereplyModel;

class Subscribereply extends AdminBase
{
    protected $subscribereply_model;
    protected $author_config;
    protected $config;
    protected $wx_user;

    protected function _initialize(){
        parent::_initialize();
        $this->wx_user = new WxUserModel;
        $this->config = $this->wx_user->find();
        $this->author_config = session::get('wechat_info');
        $this->subscribereply_model = new SubscribereplyModel;
    }

    public function index(){
        $content = $this->subscribereply_model->where('auid',$this->author_config['id'])->value('content');
        $this->assign('content',$content);
        return $this->fetch();
    }

    public function save(){
        $data = $this->request->post();
        if($this->subscribereply_model->where('auid',$this->author_config['id'])->find()){
            $result = $this->subscribereply_model->save($data,['auid'=>$this->author_config['id']]);
        }else{
            $data['auid'] = $this->author_config['id'];
            $result = $this->subscribereply_model->allowField(true)->save($data);
        }
        
        if($result){
            $this->success('保存成功');
        }else{
            $this->error('保存失败');
        }
    }

    /**
     * 推广海报
     */
    public function poster(){
        return $this->fetch();
    }

    public function savePoster(){
        if($this->request->isPost()){
            $wechatObj = new WechatThirdUtil($this->config,$this->author_config);
            $data = $this->request->post();
            dump($data);exit();
            $resoure_data = [];
            $x = $data['x'];
            $y = $data['y'];
            $image = Image::open('.'.$data['poster']);
            foreach($data['qrcode'] as $qrcode){
                $image->water('.'.$qrcode,[$x,$y])->save('.'.$qrcode);
                $resoure = $wechatObj->uploadMaterial('.'.$qrcode, $type = 'image');
                if($resoure){
                    $resoure_data[] = [
                        'auid' => $this->author_config['id'],
                        'path' => $qrcode,
                        'media_id' => $resoure['media_id'],
                        'url' => $resoure['url'],
                        'create_time' => time(),
                    ];
                }
            }
        }
    }
}